<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <span slot="before">共166条记录</span>
        <template slot="after">
          <el-button size="small" type="warning" @click="$router.push('/import')">导入</el-button>
          <el-button v-if="checkDis('exportEmployee')" size="small" type="danger" @click="exportExcl">导出</el-button>
          <el-button v-if="checkDis('addEmployee')" size="small" type="primary" @click="addStaff">新增员工</el-button>
        </template>
      </page-tools>
      <!-- 放置表格和分页 -->
      <el-card>
        <el-table border :data="staffList">
          <el-table-column label="序号" sortable="" type="index" />
          <el-table-column label="姓名" sortable="" prop="username" />
          <el-table-column label="工号" sortable="" prop="workNumber" />
          <el-table-column label="聘用形式" sortable="" prop="formOfEmployment" :formatter="employmentDetails" />
          <el-table-column label="部门" sortable="" prop="departmentName" />
          <el-table-column label="入职时间" sortable="">
            <template #default="{row}">
              {{ row.timeOfEntry | formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" sortable="" prop="enableState" />
          <el-table-column label="操作" sortable="" fixed="right" width="280" prop="">
            <template #default="{row}">
              <el-button type="text" size="small" @click="$router.push(`/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination layout="prev, pager, next" :total="total" :page-size="pages.size" @current-change="currentChange" />
        </el-row>
      </el-card>
      <dilog :show-dialog="showDialog" @close="close" />
    </div>
  </div>
</template>

<script>
import employees from '@/api/constant/employees'
import { getStaff } from '@/api/staff'
import { export_json_to_excel } from '@/vendor/Export2Excel'
import { formatDate } from '@/utils/filters'
import dilog from './component/dilog.vue'
export default {
  components: { dilog },
  data() {
    return {
      pages: {
        page: 1,
        size: 2
      },
      staffList: [],
      total: null,
      showDialog: false
    }
  },
  created() {
    this.getStaffList()
  },
  methods: {
    close() {
      this.showDialog = false
    },
    async getStaffList() {
      const { rows, total } = await getStaff(this.pages)
      this.staffList = rows
      this.total = total
    },
    employmentDetails(row, column, cellValue, index) {
      const obj = employees.hireType.find((v) => v.id === cellValue)
      return obj ? obj.value : '未知'
    },
    currentChange(page) {
      this.pages.page = page
      this.getStaffList()
    },
    addStaff() {
      this.showDialog = true
    },
    date(row, column, cellValue, index) {
      return cellValue
    },
    async exportExcl() {
      // 拿到全部员工数据
      const { rows } = await getStaff({ page: 1, size: this.total })
      console.log(rows)
      // 根据生成 excel 插件需要, 转换数据
      const header = [
        '手机号',
        '姓名',
        '入职日期',
        '聘用形式',
        '转正日期',
        '工号',
        '部门'
      ]
      const data = rows.map(userObj => {
        return this.obj2arr(userObj, header)
      })
      console.log(rows)
      console.log(data)
      // 导出
      export_json_to_excel({
        header,
        data
      })
    },
    obj2arr(userObj, header) {
      const userArr = []
      // 将用户对象里面的数据
      // 按照表头的顺序, 逐个放入到数组中
      const dict = {
        '手机号': 'mobile',
        '姓名': 'username',
        '入职日期': 'timeOfEntry',
        '聘用形式': 'formOfEmployment',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName'
      }
      header.forEach(key => {
        const enKey = dict[key]
        let value = userObj[enKey]
        if (enKey === 'formOfEmployment') {
          // 如果是聘用形式, 用枚举数据处理
          const obj = employees.hireType.find(item => item.id === value)
          value = obj ? obj.value : '未知形式'
        }
        if (enKey === 'timeOfEntry' || enKey === 'correctionTime') {
          // 格式化日期
          value = formatDate(value)
        }
        userArr.push(value)
      })
      return userArr
    }
  }
}

</script>

<style>

</style>
